package com.alibaba.otter.manager.web.service;

import com.alibaba.otter.manager.biz.config.channel.ChannelService;
import com.alibaba.otter.manager.biz.config.datamediapair.DataMediaPairService;
import com.alibaba.otter.manager.biz.statistics.table.TableStatService;
import com.alibaba.otter.manager.biz.statistics.table.param.BehaviorHistoryInfo;
import com.alibaba.otter.manager.biz.statistics.table.param.TimelineBehaviorHistoryCondition;
import com.alibaba.otter.manager.web.dto.RecentTableStatDTO;
import com.alibaba.otter.manager.web.dto.TableStatDTO;
import com.alibaba.otter.manager.web.mapstruct.TableStatMapper;
import com.alibaba.otter.shared.common.model.config.channel.Channel;
import com.alibaba.otter.shared.common.model.config.data.DataMediaPair;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Map;

@Service
public class TableStatAggService {


    private final ChannelService channelService;

    private final DataMediaPairService dataMediaPairService;

    private final TableStatService tableStatService;

    private final TableStatMapper tableStatMapper;

    public TableStatAggService(ChannelService channelService, DataMediaPairService dataMediaPairService, TableStatService tableStatService, TableStatMapper tableStatMapper) {
        this.channelService = channelService;
        this.dataMediaPairService = dataMediaPairService;
        this.tableStatService = tableStatService;
        this.tableStatMapper = tableStatMapper;
    }

    public TableStatDTO recentTableStat(Instant startTime, Instant endTime, Long dataMediaPairId) {
        if (startTime == null || endTime == null) {
            startTime = Instant.ofEpochMilli(System.currentTimeMillis() / 60000 * 60000 - 24 * 60 * 60 * 1000);
            endTime = Instant.ofEpochMilli(System.currentTimeMillis() / 60000 * 60000);
        }

        DataMediaPair dataMediaPair = dataMediaPairService.findById(dataMediaPairId);
        Channel channel = channelService.findByPipelineId(dataMediaPair.getPipelineId());
        TimelineBehaviorHistoryCondition condition = new TimelineBehaviorHistoryCondition();

        condition.setStart(startTime);
        condition.setEnd(endTime);
        condition.setPairId(dataMediaPairId);
        Map<Long, BehaviorHistoryInfo> behaviourHistoryInfos = tableStatService.listTimelineBehaviorHistory(condition);


        Long totalInsert = 0L;
        Long totalUpdate = 0L;
        Long totalDelete = 0L;
        Long totalFileCount = 0L;
        Long totalFileSize = 0L;
        for (BehaviorHistoryInfo info : behaviourHistoryInfos.values()) {
            totalInsert += info.getInsertNumber();
            totalUpdate += info.getUpdateNumber();
            totalDelete += info.getDeleteNumber();
            totalFileCount += info.getFileNumber();
            totalFileSize += info.getFileSize();
        }

        Map<Long, RecentTableStatDTO> recentTableStat = tableStatMapper.toRecentTableStat(behaviourHistoryInfos);
        TableStatDTO tableStatDTO = new TableStatDTO();

        tableStatDTO.setTotalInsert( totalInsert);
        tableStatDTO.setTotalUpdate( totalUpdate);
        tableStatDTO.setTotalDelete( totalDelete);
        tableStatDTO.setTotalFileCount( totalFileCount);
        tableStatDTO.setTotalFileSize( totalFileSize);
        tableStatDTO.setBehaviourHistoryInfos( recentTableStat);
        tableStatDTO.setStart( startTime);
        tableStatDTO.setEnd( endTime);
        tableStatDTO.setDataMediaPair( dataMediaPair);
        tableStatDTO.setChannel( channel);

        return tableStatDTO;
    }
}
